
import pymysql
import concurrent.futures
from erp_fangling_cn import image


# 查询前100条数据
def fetch_data():
    conn = pymysql.connect(
        host="43.156.247.50",  # 替换为实际的主机名
        user="root",  # 替换为实际的用户名
        password="520777",  # 替换为实际的密码
        database="house"  # 替换为实际的数据库名
    )
    sql = "SELECT house_id FROM house where house_id not in (select DISTINCT house_id from house_imgs) order by create_time desc limit 100"
    cursor = conn.cursor()
    cursor.execute(sql)
    data = cursor.fetchall()
    cursor.close()
    conn.close()
    return data

# 更新数据
def update_data(row):
    try:
        house_id = row[0]

        # 开始查询当前的房产日志信息，并入库
        flowers = image.getImages(house_id)

        conn = pymysql.connect(
            host="43.156.247.50",  # 替换为实际的主机名
            user="root",  # 替换为实际的用户名
            password="520777",  # 替换为实际的密码
            database="house"  # 替换为实际的数据库名
        )
        cursor = conn.cursor()

        if len(flowers) > 0:
            for flower in flowers:
                try:
                    # 构建插入数据的 SQL 查询
                    sql = "INSERT INTO house.house_imgs(img_id, house_id, img_type, city_id, corp_id, avatar_url, url, dept_name, sex, `size`, user_name, create_time, create_user_id) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
                    values = (
                        flower['id'],
                        house_id,
                        flower['imgType'],
                        flower['cityId'],
                        flower['corpId'],
                        flower['avatarUrl'],
                        flower['url'],
                        flower['deptName'],
                        flower['sex'],
                        flower['size'],
                        flower['userName'],
                        flower['createTime'],
                        flower['createUserId']
                    )

                    # 执行 SQL 查询
                    cursor.execute(sql, values)
                    conn.commit()
                except Exception as e:
                    print("发生异常:", str(e))
        # else:
        #     try:
        #         sql = "update house set have_logs = 0 where house_id = %s;"
        #         values = (house_id)
        #         # 执行 SQL 查询
        #         cursor.execute(sql, values)
        #         conn.commit()
        #     except Exception as e:
        #         print("发生异常:", str(e))

        cursor.close()
        conn.close()

        print(f"更新{house_id}成功{len(flowers)}")
    except Exception as e:
        print(f"未知异常:", str(e))

# 主函数
def main():
    data = fetch_data()
    while len(data) > 0:
        with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
            futures = [executor.submit(update_data, row) for row in data]
            concurrent.futures.wait(futures)


        data = fetch_data()

if __name__ == '__main__':
    main()
